﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.entity;
using shop.util;
using System.Data.SqlClient;
using System.Data;
namespace shop.dao
{
    public class CatagoryDao
    {
        public Catagory GetCatagory(int id){
            string sql = "select id, Catagory_name from catagory where id = @id";
            SqlParameter[] pms = { 
                                 new SqlParameter("@id", id),
                              
                                 };
            DataTable dt = SqlHelper.ExecuteDataTable(sql, pms);
           
            Catagory catagory  = new Catagory();
            if (dt.Rows.Count>0) {
                EntityAdapt(catagory, dt.Rows[0]);
                return catagory;
            }           
            return null;
        }
        public List<Catagory> ListCatagory()
        {
            string sql = "select id, Catagory_name from catagory";
            DataTable dt = SqlHelper.ExecuteDataTable(sql, null);
            List<Catagory> catagoryList = new List<Catagory>();
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
               Catagory catagory = new Catagory();
                EntityAdapt(catagory, dt.Rows[i]);
                catagoryList.Add(catagory);
            }

            return catagoryList;
        }
          private void EntityAdapt(Catagory catagory,DataRow row){
             catagory.Id = (int)row["id"];
             catagory.Name = (string)row["Catagory_name"];
          }
      
        public int AddCatagory(Catagory catagory) {
            string sql = "insert into catagory(Catagory_name) values(@CatagoryName) select @@identity  ";
            SqlParameter[] pms = { 
                                 new SqlParameter("@CatagoryName", catagory.Name),                               
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql,pms);           
                return count;         
        }

        public bool UpdateCatagory(Catagory catagory)
        {
            string sql = "update catagory set Catagory_name = @CatagoryName  where id =@id";
            SqlParameter[] pms = { new SqlParameter("@name", catagory.Name),
                         
                            new SqlParameter("@id", catagory.Id),
                              new SqlParameter("@CatagoryName", catagory.Name)
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql, pms);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        public bool DeleteCatagory(int id)
        {
            string sql = "delete from catagory where id = @id";
            SqlParameter[] pms = { new SqlParameter("@id", id) };
            int count = 0;
            try {
             count = SqlHelper.ExecuteNonQuery(sql, pms);
            }
            catch(Exception e) {
                Console.Write(e.Message);

            }
            if (count > 0)
            {
                return true;
            }
            return false;
        }
    }
}